package serialport;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import beans.GoodsDetailsBean;
import beans.OperateGoodsDetailsBean;
import constants.Constants;
import netframe.GetRetrofit;
import netframe.MyAction1;
import netframe.NetAddress;
import netframe.NetReconnectFunc1;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.observables.GroupedObservable;
import rx.schedulers.Schedulers;
import utils.CustomSharedPreferencePath;
import utils.FormateUtils;
import utils.TimeUtil;
import utils.ToastUtil;

/**
 * Created by admin on 2017/12/16.
 */
public class AbnoramlGoodsDeal {
    public static Map<String, OperateGoodsDetailsBean.DataBean> errorMap;

    //    //异常开门时,调用此接口
    public static void operaGoodsDetails(ArrayList<String> rfidList, final String cabinetCode, final int type, final Context context, final int selectCabinet) {
        GetRetrofit.getInstance().operateEmergencyGoodDetails(NetAddress.token, NetAddress.machineCode, rfidList, cabinetCode, type)
                .subscribeOn(Schedulers.io())
                .retryWhen(new NetReconnectFunc1())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<OperateGoodsDetailsBean>() {
                    @Override
                    public void call(OperateGoodsDetailsBean operateGoodsDetailsBean) {
                        errorMap = (Map<String, OperateGoodsDetailsBean.DataBean>) CustomSharedPreferencePath.getData(context, Constants.RECORD_ERROR_GOODS, null);
                        if (operateGoodsDetailsBean != null) {

                            List<OperateGoodsDetailsBean.DataBean> aberrantData = operateGoodsDetailsBean.getAberrantData();
                            if (aberrantData != null && aberrantData.size() != 0) {
                                List<OperateGoodsDetailsBean.DataBean> errorList = new ArrayList<>();
                                errorList = FormateUtils.getDistinctList(aberrantData, selectCabinet);
                                if (type == 8) {  // 放入
                                    addErrorGoods(context, errorList, selectCabinet);
                                } else if (type == 7) {  // 取出
                                    deleteErrorGoods(context, errorList);
                                }
                            }
                        } else {
                            ToastUtil.showToast(context, "operateGoodsDetailsBean null");
                        }

                    }
                }, new MyAction1() {
                    @Override
                    public void call(Throwable throwable) {
                        super.call(throwable);
                        ToastUtil.showToast(context, throwable.getMessage());
                    }
                });
    }

    public static void addErrorGoods(Context context, List<OperateGoodsDetailsBean.DataBean> errorGoodsList, int selectCabinet) {
        if (errorMap == null) {
            errorMap = new HashMap<>();
        }
        List<OperateGoodsDetailsBean.DataBean> list = new ArrayList<>();
        list.addAll(errorGoodsList);

        for (int i = 0; i < list.size(); i++) {
            OperateGoodsDetailsBean.DataBean dataBean = list.get(i);
            dataBean.setSelectCabinet(selectCabinet + "");
            dataBean.setErrorRecordTime(TimeUtil.getMillisecondDate());
            if (dataBean.getStatus() != 3) {
                String key = dataBean.getGID() + dataBean.getSpecification() + dataBean.getManufacturer() + dataBean.getStatus();
                if (errorMap.get(key) != null) {
                    String millisecondDate = TimeUtil.getMillisecondDate();
                    OperateGoodsDetailsBean.DataBean bean = errorMap.get(key);
                    bean.setNumber(bean.getNumber() + 1);

                    bean.setErrorRecordTime(millisecondDate);
                    bean.getBarCodeList().add(dataBean.getBarCode() + Constants.INCREASE_DECREASE_SPLIT + millisecondDate);

                    bean.setSelectCabinet(selectCabinet + "");
                    errorMap.put(key, bean);
                } else {
                    dataBean.setErrorRecordTime(TimeUtil.getMillisecondDate());
                    errorMap.put(key, dataBean);
                }
            }
        }

        CustomSharedPreferencePath.saveData(context, Constants.RECORD_ERROR_GOODS, errorMap);
    }

    public static void deleteErrorGoods(Context context, List<OperateGoodsDetailsBean.DataBean> errorGoodsList) {
        if (errorMap == null) {
            errorMap = new HashMap<>();
        }
        List<OperateGoodsDetailsBean.DataBean> list = new ArrayList<>();
        list.addAll(errorGoodsList);
        for (int i = 0; i < list.size(); i++) {
            OperateGoodsDetailsBean.DataBean dataBean = list.get(i);
            String key = dataBean.getGID() + dataBean.getSpecification() + dataBean.getManufacturer() + dataBean.getStatus();
            if (errorMap.get(key) != null) {
                OperateGoodsDetailsBean.DataBean bean = errorMap.get(key);
                if (bean.getNumber() > 1) {
                    bean.setNumber(bean.getNumber() - 1);

                    int index = 0;
                    for (int i1 = 0; i1 < bean.getBarCodeList().size(); i1++) {
                        Log.e("test", "deleteErrorGoods: " + bean.getBarCodeList().get(i1).split(Constants.INCREASE_DECREASE_SPLIT)[0] );
                        if (bean.getBarCodeList().get(i1).split(Constants.INCREASE_DECREASE_SPLIT)[0].equals(dataBean.getBarCode())){
                            index = i1;
                        }
                    }
                    bean.getBarCodeList().remove(index);
                    errorMap.put(key, bean);
                } else {
                    errorMap.remove(bean);
                }
            }
        }

        CustomSharedPreferencePath.saveData(context, Constants.RECORD_ERROR_GOODS, errorMap);
    }

}
